package cn.itmantzy.org.service.impl;

import cn.itmantzy.basic.exception.BusinessException;
import cn.itmantzy.basic.service.impl.BaseServiceImpl;
import cn.itmantzy.org.domain.Employee;
import cn.itmantzy.org.domain.Shop;
import cn.itmantzy.org.mapper.EmployeeMapper;
import cn.itmantzy.org.mapper.ShopMapper;
import cn.itmantzy.org.service.IEmployeeService;
import cn.itmantzy.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IEmployeeService employeeService;

    @Override
    public void settlement(Shop shop) {
        // 1.校验两次密码输入是否一致，不一致抛出异常
        // 获取到店长信息
        Employee admin = shop.getAdmin();
        // 判断两次输入的密码是否一致(逆向思维，不一致，直接抛出业务异常)
        if (!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("两次输入密码不一致，请重新输入，保证两次密码一致！！！");
        }
        // 2.校验用户信息是否已经存在，username，email，phone
        // 两次密码一致，现在校验用户是否已经存在
        Employee employee = employeeMapper.queryByUsername(admin);
        if (employee != null){
            throw new BusinessException("用户信息已存在，请直接登录或点击忘记密码，进行找回密码操作！！！");
        }
        // 3.如果用户不存在，就将employee信息保存，并返回自增id(因为shop表格中admin_id不能为空)
        employeeService.add(admin);
        // 将admin中自增的id保存到shop对象中
        shop.setAdmin(admin);
        // 4.将id存入shop表格中，返回shop自增id
        shopMapper.save(shop);
        // 5.将返回的shop保存修改shop_id
        // 将shop的id赋值给admin
        admin.setShop_id(shop.getId());
        // 修改employee中的shop_id
        employeeService.update(admin);
    }
}
